import { minimatch } from "minimatch";
const PAGES_PATH = "/src/pages";
const EXCLUDE_PAGES = [
  "/index*jsx",
  "/login/index.jsx",
  "/error/**/*jsx",
  "/setting/index.jsx",
  "/theme/index.jsx",
  "/file/index.jsx",
  "/**/components/**/*.jsx",
  "/**/export/*.jsx",
  "/system/menu/import.jsx",
];
const PAGE_JSON_SUFFIX = "/page.json";

export const pagesModules = import.meta.glob("@/pages/**/*.jsx");

export const pagesJson = import.meta.glob("@/pages/**/page.json", {
  eager: true,
  import: "default",
});

const excludeDispose = (path) => {
  for (const rule of EXCLUDE_PAGES) {
    if (minimatch(path, PAGES_PATH + rule)) {
      return true;
    }
  }
  return false;
};

export const simpleModules = () => {
  const catalogs = simpleCatalogModules();
  const menus = simpleMenuModules();
  return catalogs.concat(menus).sort((a, b) => a.key.localeCompare(b.key));
};

export const simpleMenuModules = () => {
  return Object.entries(pagesModules)
    .filter(([path]) => !excludeDispose(path))
    .map(([path]) => createMenu(path.replace(PAGES_PATH, "")));
};

export const createMenu = (path) => {
  const jsonPath = path.replace("/index.jsx", PAGE_JSON_SUFFIX);
  const { title: menuName, icon: menuIcon, ...meta } = pagesJson[PAGES_PATH + jsonPath] || {};

  const component = path.replace(".jsx", "");
  const menuCode = component.replace("index", "").split("/").filter(Boolean).join(":").replace("-", ":");
  const menuPath = "/" + path.replace("/index.jsx", "").replace(".jsx", "").split("/").pop();
  meta.buttons = [
    { menuName: "添加数据", menuCode: `${menuCode}:add` },
    { menuName: "修改数据", menuCode: `${menuCode}:update` },
    { menuName: "删除数据", menuCode: `${menuCode}:delete` },
    { menuName: "导出数据", menuCode: `${menuCode}:export` },
  ];
  delete meta.children;

  return {
    key: path,
    component,
    menuPath,
    menuCode,
    menuName,
    menuIcon,
    ...meta,
    menuType: "MENU",
    menuTypeName: "菜单",
  };
};
export const createMenus = (paths) => {
  return paths.map((path) => {
    return path.endsWith(".jsx") ? createMenu(path) : createCatalog(path);
  });
};

const simpleCatalogModules = () => {
  const map = new Map();

  return Object.entries(pagesModules)
    .filter(([path]) => {
      const pathname = path.split("/").reverse().splice(2).reverse().join("/") + "/index.jsx";
      const status = excludeDispose(path) || pagesModules[pathname] || map.has(pathname);
      if (map.has(pathname) === false) {
        map.set(pathname, true);
      }
      return !status;
    })
    .map(([path]) => {
      path = path.replace(PAGES_PATH, "").split("/").reverse().splice(2).reverse().join("/");
      return createCatalog(path);
    });
};

const createCatalog = (path) => {
  const jsonPath = path + PAGE_JSON_SUFFIX;
  const { title: menuName, icon: menuIcon, ...meta } = pagesJson[PAGES_PATH + jsonPath] || {};
  const menuPath = "/" + path.split("/").pop();

  return {
    key: path,
    menuPath,
    menuName,
    menuIcon,
    ...meta,
    menuType: "CATALOG",
    menuTypeName: "目录",
  };
};
